// 会员中心
bangkeapp.controller('memberhome', 
["$scope", "accountService", "userinfoService","memberService",  "$ionicLoading", "$interval",
	"$ionicNavBarDelegate", "fileurl","$timeout",
    function($scope, accountService, userinfoService, memberService,$ionicLoading, $interval,
    		$ionicNavBarDelegate, fileurl,$timeout) {
    		$ionicNavBarDelegate.showBackButton('hide');
       	$scope.userinfo = userinfoService.getUser();
       	$scope.fileurl = fileurl;
        
    		var flag = [];
    		$ionicLoading.show({
			content: 'Loading',
			animation: 'fade-in',
			showBackdrop: true,
			maxWidth: 200,
			showDelay: 0
		});
		if ("01" == $scope.userinfo.verifystatus) {
		
	        $scope.account = {};
	        accountService.account().then(function(result) {
	            $scope.account = result;
	        		$ionicLoading.hide();
	        }, function(error) {
	        }, function(progress) {
	        });
        } else {
        		$ionicLoading.hide();
        }
        $scope.tofood = function(){
        		// 美食跳转
        		// window.location.href = "http://localhost:8088";
        }
        
        $scope.format = function(s) {
        		return s < 10 ? '0' + s: s;
    		}
        
		$interval(function(){
			var now = new Date();
			$scope.time = now.getFullYear() +
				"-" + $scope.format((now.getMonth() + 1)) + "-" + $scope.format(now.getDate()) +
				"  " + $scope.format(now.getHours()) + ":" + $scope.format(now.getMinutes()) +
				":" + $scope.format(now.getSeconds());
		}, 1000, 0);
        //签到
		$scope.toSign=function () {
			$ionicLoading.show({
				content: 'Loading',
				animation: 'fade-in',
				showBackdrop: true,
				maxWidth: 200,
				showDelay: 0
			});
			memberService.toSign().then(function(data) {
				if(data.result=='1'){
					$scope.isSign=true;
				}
				$ionicLoading.hide();
			}, function(error) {
			}, function(progress) {
			});
			$timeout(function () {
				$ionicLoading.hide();
			},3000)
		};
		//签到查询
		$scope.sign=function () {
			$ionicLoading.show({
				content: 'Loading',
				animation: 'fade-in',
				showBackdrop: true,
				maxWidth: 200,
				showDelay: 0
			});
			memberService.sign().then(function(data) {
				if(data.signinFlag=='1'){
					$scope.isSign=true;
				}
				else {
					$scope.isSign=false;
				}
				$ionicLoading.hide();
			}, function(error) {
			}, function(progress) {
			});
			$timeout(function () {
				$ionicLoading.hide();
			},3000)
		};
		//初始化函数
		var init=function () {
			$scope.isSign=false;
			$scope.sign();
		};
		init();
    }
]);
// 我的推荐人
bangkeapp.controller('referee', 
	["$scope", "userinfoService", "memberService", "$ionicLoading", "$state", "$ionicModal",
    function($scope, userinfoService, memberService, $ionicLoading, $state, $ionicModal) {
    	
    		$ionicLoading.show({
			content: 'Loading',
			animation: 'fade-in',
			showBackdrop: true,
			maxWidth: 200,
			showDelay: 0
		});
		
        $scope.refereece = {};
        $scope.showflag = true;
        $scope.saveflag = true;
        var member = userinfoService.getUser();
		if (undefined != member.reference && "" != member.reference){
	    		// 请求获取我的推荐人
	    		memberService.myreference().then(function(result) {
	        		$scope.refereece = result;
	    			$ionicLoading.hide();
	        }, function(error) {
	        }, function(progress) {
	        		if (progress){
		    			$scope.showflag= true;
		    			$scope.saveflag = true;
	    			} else {
	    				$scope.showflag = false;
	        			$scope.saveflag = false;
	    			}
	        });
        }
		
		// 修改推荐人模块
        $ionicModal.fromTemplateUrl('refereeedit.html', {
            scope: $scope
        }).then(function(modal) {
            $scope.modal = modal;
        });
		
		// 修改完成
        $ionicModal.fromTemplateUrl('refereecommit.html', {
            scope: $scope
        }).then(function(modal) {
            $scope.commitmodal = modal;
        });
		
		// 修改推荐人
		$scope.toedit = function(){
			$scope.modal.show();
		}
		$scope.goback = function(){
			$scope.modal.hide();
		}
		$scope.goback1 = function(){
			$scope.commitmodal.hide();
		}
		
    		$scope.newrefereece = {};
        
        $scope.nextstep = function(){
	        	$ionicLoading.show({
				content: 'Loading',
				animation: 'fade-in',
				showBackdrop: true,
				maxWidth: 200,
				showDelay: 0
			});
        		var name = $scope.newrefereece.name;
        		// 获取要转账对象会员的信息
	    		memberService.getmemberinfo(name).then(function(result) {
	    			$ionicLoading.hide();
	    			if (result.length == 0) {
	    				alert("用户不存在");
	    			} else if (result.length > 1) {
	    				
	    			} else {
	    				$scope.modal.hide()
	    				$scope.commitmodal.show();
	    				$scope.showflag = true;
	    				$scope.newrefereece = result[0];
	    				$scope.saveflag = false;
	    			}
	        }, function(error) {
	        }, function(progress) {
	        });
        }
        $scope.saverefereece = function(){
        		if ($scope.refereece.memberid == $scope.newrefereece.memberid) {
        			alert("您未修改推荐人!");
        			return false;
        		}
        		$ionicLoading.show({
				content: 'Loading',
				animation: 'fade-in',
				showBackdrop: true,
				maxWidth: 200,
				showDelay: 0
			});
        		memberService.savereference($scope.newrefereece.memberid).then(function(result) {
        			var member = userinfoService.getUser();
        			member.reference = $scope.newrefereece.memberid;
        			userinfoService.setUser(member);
	    			$ionicLoading.hide();
	        }, function(error) {
	        }, function(progress) {
	        		if (progress) {
	        			alert("保存成功");
	        			$scope.saveflag = true;
	        			$scope.saveok = true;
	        		} else {
	        			alert("保存失败!");
	        		}
	        		
	        });
	        $scope.comlet = function(){
				$scope.commitmodal.hide();
				// 请求获取我的推荐人
		    		memberService.myreference().then(function(result) {
		        		$scope.refereece = result;
		    			$ionicLoading.hide();
		        }, function(error) {
		        }, function(progress) {
		        		if (progress){
			    			$scope.showflag= true;
			    			$scope.saveflag = true;
		    			} else {
		    				$scope.showflag = false;
		        			$scope.saveflag = false;
		    			}
		        });
	        }
        }
    }
]);

// 会员列表
bangkeapp.controller('memberlist', 
	["$scope", "memberService",
    function($scope, memberService) {

        // 获取会员列表信息
        memberService.memberlist().then(function(result) {
            $scope.list = result;
        }, function(error) {
            $scope.list = "error!";
        }, function(progress) {

        });
    }
]);

// 我要升级
bangkeapp.controller('toupgrademember', 
	["$scope", "$state", "userinfoService",
    function($scope, $state, userinfoService){
    		$scope.member = userinfoService.getUser();
    }
]);
// 会员升级
bangkeapp.controller('upgrademember', 
	["$scope", "$state", "$ionicLoading", "userinfoService", "memberService",
    function($scope, $state, $ionicLoading, userinfoService, memberService){
    		var member = userinfoService.getUser();
    		
    		if (undefined != member.memberid && '' != member.memberid) {
	    		// 加载动画
			$ionicLoading.show({
				content: 'Loading',
				animation: 'fade-in',
				showBackdrop: true,
				maxWidth: 200,
				showDelay: 0
			});
			$scope.redpacket = "";
	    		// 查询会员积分
			memberService.redpacket().then(function(result) {
	            console.log(angular.fromJson(result.redpacket));
	            $scope.redpacket = result.redpacket;
	            $ionicLoading.hide();
	        }, function(error) {
	            $scope.list = "error!";
	        }, function(progress) {
	        		$ionicLoading.hide();
	        });
	    		
	    		// 升级为高级会员
	    		$scope.silver = function(){
	    			// 加载动画
				$ionicLoading.show({
					content: 'Loading',
					animation: 'fade-in',
					showBackdrop: true,
					maxWidth: 200,
					showDelay: 0
				});
				member.memberlevel = "02";
				memberService.upgrademember(member).then(function(result) {
		            
		            $ionicLoading.hide();
		            $state.go('bangke.memberhome', {});
		        }, function(error) {
		            $scope.list = "error!";
		        }, function(progress) {
		        		if (progress) {
		        			alert("升级成功!");
		        		}
		        });
	    		}
	    		// 升级为商户会员
	    		$scope.golden = function(){
	    			// 加载动画
				$ionicLoading.show({
					content: 'Loading',
					animation: 'fade-in',
					showBackdrop: true,
					maxWidth: 200,
					showDelay: 0
				});
				member.memberlevel = "03";
				memberService.upgrademember(member).then(function(result) {
		            $ionicLoading.hide();
		            $state.go('bangke.memberhome', {});
		        }, function(error) {
		            $scope.list = "error!";
		        }, function(progress) {
		        		if (progress) {
		        			alert("升级成功!");
		        		}
		        });
	    		}
	    	}
    }
]);

// 设置支付密码
bangkeapp.controller("initpaypwdctrl", 
["$scope", "userinfoService", "memberService", "$interval", "$state", "$stateParams", 
    function($scope, userinfoService, memberService, $interval, $state, $stateParams) {
    		
    		$scope.inurl = $stateParams.inurl;
    		
    		$scope.user = userinfoService.getUser();
    		$scope.text = "获取验证码";
    		$scope.initpaypwd = {};
    		
    		// 获取验证码
    		$scope.getsmscode = function(){
    			
			if (undefined == $scope.user.tel || "" == $scope.user.tel) {
				alert("手机号不能为空!");
				return false;
			}
			var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/; 
			if(!myreg.test($scope.user.tel)) { 
			    alert('不是有效手机号！'); 
			    return false; 
			} 
			$scope.tel = $scope.user.tel;
			
			
			$scope.n = 60;
	        $scope.text = $scope.n + "秒";
	        $scope.isDisable = true;
	
	        var time = $interval(function () {
	
	            $scope.n--;
	            $scope.text = $scope.n + "秒后可重新获取";
	            if ($scope.n == 0)
	            {
	                $interval.cancel(time);
	                $scope.isDisable = false;
	                $scope.text = "获取验证码";
	            }
	
	        },1000);

    			memberService.getsmscode($scope.user.tel).then(function(result) {
	    		}, function(error) {
	        }, function(progress) {
	        });
    		}
    		
    		$scope.savepaypwd = function(){
    			// 校验
    			if (undefined == $scope.initpaypwd.securepwd || "" == $scope.initpaypwd.securepwd){
    				return false;
    			}
    			if (undefined == $scope.initpaypwd.shortCode || "" == $scope.initpaypwd.shortCode){
    				return false;
    			}
    			if (undefined == $scope.initpaypwd.surepwd || "" == $scope.initpaypwd.surepwd){
    				return false;
    			}
    			if ($scope.initpaypwd.surepwd != $scope.initpaypwd.securepwd) {
    				alert("支付密码不一致");
    				return false;
    			}
    			var body = {
	    			isCheckOld: "0",// 是否验证原始安全密码，必填，0：否，不校验原始密码；1：是，校验原始密码；
	        		isShortCode: "1",// 是否验证短信码，必填， 0：否，不需要；1：是，需要；
	        		memberid: $scope.user.memberid,// 会员编号
	        		oldSecurepwd: "",// 原始安全密码
	        		securepwd: $scope.initpaypwd.securepwd,// 新安全密码
	        		surepwd: $scope.initpaypwd.surepwd,// 二次确认安全密码
	        		shortCode: $scope.initpaypwd.shortCode // 短信验证码
	        }
    			memberService.savepaypwd(body).then(function(result) {
    				if ('Y' == result.body.operateStatus) {
    					alert("设置成功");
    					$state.go($scope.inurl);
    				} 
	    		}, function(error) {
	        }, function(progress) {
	        });
    		}
    }
]);
// 签到详情页面
bangkeapp.controller('signctrl',
	["$scope", "$state", "userinfoService","memberService","$ionicLoading","$timeout","uiCalendarConfig","$interval",
		function($scope, $state, userinfoService,memberService,$ionicLoading,$timeout,uiCalendarConfig,$interval){
			//
			$scope.calendar=function () {
				var date = new Date();
				var d = date.getDate();
				var m = date.getMonth();
				var y = date.getFullYear();


				/* event source that contains custom events on the scope */
				// $scope.events = [
				// 	{title: 'All Day Event',start: new Date(y, m, 1)},
				// 	{title: 'Long Event',start: new Date(y, m, d - 5),end: new Date(y, m, d - 2)},
				// 	{id: 999,title: 'Repeating Event',start: new Date(y, m, d - 3, 16, 0),allDay: false},
				// 	{id: 999,title: 'Repeating Event',start: new Date(y, m, d + 4, 16, 0),allDay: false},
				// 	{title: 'Birthday Party',start: new Date(y, m, d + 1, 19, 0),end: new Date(y, m, d + 1, 22, 30),allDay: false},
				// 	{title: 'Click for Google',start: new Date(y, m, 28),end: new Date(y, m, 29),url: 'http://google.com/'},
				// 	{title:'zql',start:new Date(y,m,3)}
				// ];
				/* config object */
				$scope.uiConfig = {
					calendar:{
						height: 450,
						editable: false,
						header:{
							left: '',
							center: 'title',
							right: 'today prev,next'
						}
					}
				};

				/* event sources array*/
				$scope.eventSources = [$scope.events];
			};
			//签到
			$scope.signList=function () {
				$ionicLoading.show({
					content: 'Loading',
					animation: 'fade-in',
					showBackdrop: true,
					maxWidth: 200,
					showDelay: 0
				});
				memberService.signList().then(function(data) {
					if(data.signin){
						$scope.signin=data.signin;
						$scope.strNo=data.strNo;
						$scope.sumReward=data.sumReward;
						var date = new Date();
						var d = date.getDate();
						var m = date.getMonth();
						var y = date.getFullYear();
						if($scope.signin){
							$.each($scope.signin,function (index,data) {
								var day=Number(data.signdate.substr(8,2));
								var sign={title: '已签到',start: new Date(y, m, day)};
								$scope.events.push(sign);
							});
						}


					}
					$ionicLoading.hide();
				}, function(error) {
				}, function(progress) {
				});
				$timeout(function () {
					$ionicLoading.hide();
				},3000)
			};
			//初始化函数
			var init=function () {
				$scope.format = function(s) {
					return s < 10 ? '0' + s: s;
				}
				var now = new Date();
				$scope.time = now.getFullYear() + "-" + $scope.format((now.getMonth() + 1)) + "-" + $scope.format(now.getDate());
				$scope.signin=[];
				$scope.strNo='';
				$scope.sumReward='';
				$scope.events = [];
				$scope.signList();
				$timeout(
					$scope.calendar(),1000
				)
				};
			init();
		}
	]);